using System;
using System.Collections.Generic;
namespace Framework
{
    public static class EventDispatcherAsync
    {
        private static readonly Queue<Action> _nextFrameCallAddList = new Queue<Action>();
        private static readonly Queue<Action> _nextFrameCallExecuteList = new Queue<Action>();
        public static void Add(Action value)
        {
            lock (_nextFrameCallAddList)
            {
                _nextFrameCallAddList.Enqueue(value);
            }
        }
        public static void Update(float deltaTime, float unscaledDeltaTime)
        {
            int len = _nextFrameCallExecuteList.Count;
            while (len-- > 0)
            {
                var item = _nextFrameCallExecuteList.Dequeue();
                item.Invoke();
            }
            len = _nextFrameCallAddList.Count;
            while (len-- > 0)
            {
                var item = _nextFrameCallAddList.Dequeue();
                _nextFrameCallExecuteList.Enqueue(item);
            }
        }
    }
}